//
//  ViewoFootView.m
//  diaoyuren
//
//  Created by Cheng on 16/1/22.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ViewoFootView.h"
#import "VideoDescCell.h"
#import "NewsCell.h"
#import "CommentCell.h"
#import "Manager.h"
#import "NSString+ClearPPPP.h"
#import "NSString+HtmlCharact.h"
#import "ChangyanSDK.h"
#import "NewsCell.h"
#import "NewsSectionHeadView.h"
@interface ViewoFootView()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,ClickLabel>

@property (nonatomic,strong) UIView *slideView;

@property (nonatomic,strong)UIView *kongbaiView;//顶部空白区域
@property (nonatomic,strong) UIView *btnView;

@end

@implementation ViewoFootView

-(NSMutableArray *)relateArr
{
    if (!_relateArr) {
        _relateArr=[NSMutableArray array];
    }
    return _relateArr;
}
-(UIView *)slideView{
    if (!_slideView) {
        _slideView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH/2-SCREEN_WIDTH/3)/2, 48.7,SCREEN_WIDTH/3, 1.3)];
        [self.btnView addSubview:_slideView];
    }
   
    return _slideView;
}
-(UIView *)kongbaiView{
    if (!_kongbaiView) {
        _kongbaiView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        [self addSubview:_kongbaiView];
    }

    return _kongbaiView;
}

-(UIView *)btnView{
    if (!_btnView) {
        _btnView = [[UIView alloc]initWithFrame:CGRectMake(0, Height(self.kongbaiView), SCREEN_WIDTH, 50)];
        _btnView.backgroundColor = [UIColor whiteColor];
        
        UIView *bomLin = [[UIView alloc]initWithFrame:CGRectMake(0, 49.3, SCREEN_WIDTH, 0.5)];
        
        bomLin.backgroundColor = linColor;
        [_btnView addSubview:bomLin];
        
        UIView *linView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-0.7)/2, 10, 0.3, H(_btnView)-20)];
        linView.backgroundColor = linColor;
        [_btnView addSubview:linView];
        
        [self addSubview:_btnView];
    }
    return _btnView;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        self.kongbaiView.backgroundColor = [UIColor clearColor];
        [self creatScrollview];        
        self.FootHight = 200;
        
        }
    return self;
}

#pragma mark -ClickLabe协议
-(void)clickLabel:(DetailModel *)model
{
    [self.delegate tableviewFresh:YES];
}

-(void)CreatBtn{
    for (int i=0; i<2; i++) {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame=CGRectMake(SCREEN_WIDTH/2*i, 2,SCREEN_WIDTH/2, 46);
        [btn setTitle:@[@"视频简介",@"相关视频"][i] forState:UIControlStateNormal];
        [self.btnView addSubview:btn];
        btn.titleLabel.font = font(SCREEN_WIDTH*0.05);
        btn.backgroundColor=[UIColor clearColor];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag=100+i;
        if (i==0) {
            [btn setTitleColor:mainColor forState:UIControlStateNormal];
        }
        
    }
}
-(void)creatScrollview
{
    [self.scrollview removeFromSuperview];
    self.scrollview=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, H(self)-70)];
    self.scrollview.contentSize=CGSizeMake(SCREEN_WIDTH*2, 0);
    self.scrollview.showsVerticalScrollIndicator=NO;
    self.scrollview.showsHorizontalScrollIndicator=NO;
    self.scrollview.pagingEnabled=YES;
    self.scrollview.delegate=self;
    self.scrollview.tag=10000;
    [self addSubview:self.scrollview];
    [self creatTableView];
    [self CreatBtn];
    self.slideView.backgroundColor = mainColor;
}
-(void)creatTableView{
    for (int i=0; i<2; i++) {
        
        UITableView * tableview=[[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH,SCREEN_HEIGHT-114) style:UITableViewStylePlain];
        tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
        tableview.tableFooterView=[[UIView alloc] init];
        tableview.tag=i+10;
        tableview.showsHorizontalScrollIndicator=NO;
        tableview.showsVerticalScrollIndicator=NO;
        tableview.bounces = NO;
        tableview.scrollEnabled = NO;
        if (tableview.tag==10) {
            [tableview registerClass:[VideoDescCell class] forCellReuseIdentifier:@"VideoDescCell"];
        }else{
            [tableview registerClass:[NewsCell class] forCellReuseIdentifier:@"NewsCell"];
        }
        
        tableview.delegate=self;
        tableview.dataSource=self;
        [self.scrollview addSubview:tableview];
    }
}
-(void)btnAction:(UIButton*)btn
{
    [self.scrollview setContentOffset:CGPointMake(SCREEN_WIDTH*(btn.tag-100), 0) animated:YES];
    for (int i=0; i<2; i++) {
        UIButton *Btn=(UIButton*)[self viewWithTag:100+i];
        if (Btn.tag==btn.tag) {
            [Btn setTitleColor:mainColor forState:UIControlStateNormal];
            self.slideView.centerX = Btn.centerX;
        }else{
            [Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
    }
    if (btn.tag==101) {
        [self.delegate RelateTableView];
    }else{
        [self.delegate tableviewFresh:NO];
    }
}

#pragma mark - scrollview代理方法
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pianyi = scrollView.contentOffset.x;
    if (pianyi==SCREEN_WIDTH) {
        [self.delegate RelateTableView];
    }else{
        [self.delegate tableviewFresh:NO];
    }
    
    if (scrollView.tag==10000) {
        for (int i=0; i<2; i++) {
            UIButton *Btn=(UIButton*)[self viewWithTag:100+i];
            if (scrollView.contentOffset.x/SCREEN_WIDTH==Btn.tag-100) {
                [Btn setTitleColor:mainColor forState:UIControlStateNormal];
                  self.slideView.centerX = Btn.centerX;
            }else{
                [Btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                            }
        }
    }
}
-(void)setRelateHeight:(NSInteger)relateHeight
{
    _relateHeight=relateHeight;
    self.scrollview.frame=CGRectMake(0, 70, SCREEN_WIDTH, self.height-50);
    UITableView *tableview=(UITableView*)[self viewWithTag:11];
    tableview.frame=CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, relateHeight);
    [tableview reloadData];
}
-(void)setVideoDescCellheight:(NSInteger)VideoDescCellheight
{
    _VideoDescCellheight=VideoDescCellheight;
    self.scrollview.frame=CGRectMake(0, 70, SCREEN_WIDTH,self.height-50);
    UITableView *tableview=(UITableView*)[self viewWithTag:10];
    
    [tableview reloadData];
    
}
#pragma mark - tableview代理方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView.tag==11) {
       
        return self.relateArr.count;
    }
        return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView.tag==10) {
        VideoDescCell *cell=[tableView dequeueReusableCellWithIdentifier:@"VideoDescCell"];
        [cell creatlabel:self.VideoDescCellheight-50];
        
        cell.detaomodel= self.model;;
        cell.delegate=self;
        return cell;
    }else{
        NewsCell *cell= [tableView dequeueReusableCellWithIdentifier:@"NewsCell"];;// [tableView  cellForRowAtIndexPath:indexPath];
//        if (!cell) {
//            cell = [[NewsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCell"];
//        }
       
        cell.model = self.relateArr[indexPath.row];

        return cell;
    }
    return nil;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.tag==11) {
        //相关视频的cell
        if (kDIEIsIPhone6P) {
            return 105;
        }else if (kDIEIsIPhone6){
            return 97;
        }
        
        else{
            return 85;
        }
    }
    return self.VideoDescCellheight-50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (tableView.tag==11) {
        NEWAticListModel *model = self.relateArr[indexPath.row];
     
        [self.delegate SelectRelateTablrView:model.AtcID Skin:model.skin];
    }
}

@end
